﻿// <copyright file="MovieReviewsAndCriticsPicks.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-04-08</date>
// <summary>NYT Movie Reviews and Critics Picks API</summary>

namespace NYTimes
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Text;
    using NYTimes.Core;
    using NYTimes.Data;

    /// <summary>
    /// Movie Review Resource type
    /// </summary>
    public enum ResourceType
    {
        /// <summary>
        /// All Resources
        /// </summary>
        All,

        /// <summary>
        /// Critics Picks
        /// </summary>
        Picks,

        /// <summary>
        /// the DVD Picks
        /// </summary>
        DVDPicks
    }

    /// <summary>
    /// New York Times Movie Review and Critics Picks API
    /// </summary>
    /// <remarks>http://developer.nytimes.com/docs/movie_reviews_api#h3-reviews</remarks>
    public class MovieReviewsAndCriticsPicks : MovieReviewBase
    {
        #region Fields
        /// <summary>
        /// the resource type
        /// </summary>
        private ResourceType resourceType;

        /// <summary>
        /// the sort order
        /// </summary>
        private MovieReviewOrder order;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the resource type to return
        /// </summary>
        [Description("Specify All, Picks, or DVD Picks for the type of results to return.")]
        public ResourceType ResourceType 
        { 
            get
            {
                return this.resourceType;
            } 
            
            set
            {
                this.resourceType = value;

                this.UpdateResults();
            }
        }

        /// <summary>
        /// Gets or sets the order of the results
        /// </summary>
        [Description("Specify the sort order of the results.")]
        public MovieReviewOrder? Order
        {
            get
            {
                return this.order;
            }

            set
            {
                this.order = value.Value;

                this.UpdateResults();
            }
        }

        #endregion

        #region Methods
        /// <summary>
        /// Generates the URI for the REST Call
        /// </summary>
        /// <returns>the URI or null</returns>
        public override Uri GenerateUri()
        {
            var builder = new StringBuilder("http://api.nytimes.com/svc/movies/v2/reviews/");

            string resourceTypeName = string.Empty;

            switch (this.ResourceType)
            {
                case ResourceType.All:
                    resourceTypeName = "all";
                    break;

                case ResourceType.Picks:
                    resourceTypeName = "picks";
                    break;

                case ResourceType.DVDPicks:
                    resourceTypeName = "dvd-picks";
                    break;
            }
            
            builder.AppendFormat(CultureInfo.InvariantCulture, "{0}.xml?api-key={1}", resourceTypeName, this.ApiKey);

            if (this.Offset > 0)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "&offset={0}", this.Offset);
            }

            if (this.Order.HasValue)
            {
                builder.AppendFormat(
                    CultureInfo.InvariantCulture,
                    "&order={0}",
                    MovieReviewBase.ReviewOrderMap[this.Order.Value]);
            }

            return new Uri(builder.ToString(), UriKind.Absolute);
        }
        #endregion
    }
}
